package com.yueqian.framework.annotation.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.yueqian.framework.exception.ValidatorException;

import cn.hutool.core.util.ArrayUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class EnumValidator implements ConstraintValidator<Enum, Integer> {
	private int[] enumArrays;

	@Override
	public void initialize(Enum constraintAnnotation) {
		enumArrays = constraintAnnotation.value();
	}

	@Override
	public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
		if (!ArrayUtil.contains(enumArrays, value)) {
			throw new ValidatorException(constraintValidatorContext.getDefaultConstraintMessageTemplate());
		}
		return true;
	}
}
